/*
 * spi.c
 *
 * Created: 28.10.2011 17:23:05
 *  Author: Fabian
 */ 



#include "spi.h"




//***********MASTER_SETTING*************
void spi_init_master(void)
{
	/* Set MOSI, SS and SCK output, MISO input*/
	DDR_SPI |= (1<<DD_MOSI);
	DDR_SPI |= (1<<DD_SCK);
	DDR_SPI |= (1<<DD_SS);
	DDR_SPI &= ~(1<<DD_MISO);
	PORT_SPI |= (1<<DD_SS);
	
	
	/* enable spi, Master, set clock rate fck/16 */
	SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
	
}


//********SLAVE-SETTING*****************
void spi_init_slave(void)
{
	/* Set MISO output, MOSI input */
	DDR_SPI |= (1<<DD_MISO);
	DDR_SPI &= ~(1<<DD_MOSI);
	PORT_SPI |= (1 << DD_MOSI); // pull-up / input
	DDR_SPI &= ~(1<<DD_SS);
	PORT_SPI |= (1<<DD_SS);
	/* Enable SPI */
	SPCR = (1<<SPE);
}

/**************************************************        
Function: SPI_RW();                                         
                                                            
Description:                                                
	Writes one UINT8 to RFM70, and return the UINT8 read 
**************************************************/        
unsigned char SPI_RW(unsigned char value)                                    
{                                                           
	SPDR = value;					//Load byte to Data register
	while(!(SPSR & (1<<SPIF))); 	// Wait for transmission complete 
	return(SPDR);           		  // return read UINT8
}         
